﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Koonsoft.HYCRM.Model;

namespace Koonsoft.HYCRM.DAL
{
    /// <summary>
    /// 菜单访问数据层
    /// </summary>
    public class MenuDAL
    {
        /// <summary>
        /// 根据登录名获取菜单
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public static List<MenuInfo> GetMenusByEmpolyeeID(string loginName)
        {
            var menus = new List<MenuInfo>();
            SqlDataReader sqlrd;

            const string sql = @"Menu_GetTopMenu";

            var sqlconn = new SqlConnection
            {
                ConnectionString = ConfigurationManager.ConnectionStrings["KOONCRMDB"].ConnectionString
            };

            var sqlcmd = new SqlCommand
            {
                CommandText = sql,
                CommandType = CommandType.StoredProcedure,
                Connection = sqlconn
            };

            sqlcmd.Parameters.Add("@empolyeeLoginName", SqlDbType.NVarChar, 16);
            sqlcmd.Parameters["@empolyeeLoginName"].Value = loginName;

            try
            {
                sqlcmd.Connection.Open();

                sqlrd = sqlcmd.ExecuteReader();

                while (sqlrd.Read())
                {
                    var menu = new MenuInfo
                    {
                        MenuTitle = sqlrd["MenuName"].ToString(),
                        MenuID = Convert.ToInt32(sqlrd["MenuID"]),
                        URL = sqlrd["URL"].ToString()
                    };

                    menus.Add(menu);
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                sqlcmd.Connection.Close();
            }

            return menus;
        }

        /// <summary>
        /// 根据员工编号获取二级子菜单
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="parentMenuID"></param>
        /// <returns></returns>
        public static List<MenuInfo> GetSubMenusByEmpolyeeID(string loginName, int parentMenuID)
        {
            var menus = new List<MenuInfo>();
            SqlDataReader sqlrd;

            const string sql = @"Menu_GetSubMenu";

            var sqlconn = new SqlConnection
            {
                ConnectionString = ConfigurationManager.ConnectionStrings["KOONCRMDB"].ConnectionString
            };

            var sqlcmd = new SqlCommand
            {
                CommandText = sql,
                CommandType = CommandType.StoredProcedure,
                Connection = sqlconn
            };

            sqlcmd.Parameters.Add("@empolyeeLoginName", SqlDbType.NVarChar, 16);
            sqlcmd.Parameters["@empolyeeLoginName"].Value = loginName;

            sqlcmd.Parameters.Add("@parentmenuid", SqlDbType.Int);
            sqlcmd.Parameters["@parentmenuid"].Value = parentMenuID;

            try
            {
                sqlcmd.Connection.Open();

                sqlrd = sqlcmd.ExecuteReader();

                while (sqlrd.Read())
                {
                    var menu = new MenuInfo
                    {
                        MenuTitle = sqlrd["MenuName"].ToString(),
                        MenuID = Convert.ToInt32(sqlrd["MenuID"]),
                        URL = sqlrd["URL"].ToString()
                    };

                    menus.Add(menu);
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                sqlcmd.Connection.Close();
            }

            return menus;
        }

        /// <summary>
        /// 检查是否有权限登录
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool HasPermission(string loginName, string url)
        {
            SqlDataReader sqlrd;

            const string sql = @"Menu_CheckPermission";

            var sqlconn = new SqlConnection
            {
                ConnectionString = ConfigurationManager.ConnectionStrings["KOONCRMDB"].ConnectionString
            };

            var sqlcmd = new SqlCommand
            {
                CommandText = sql,
                CommandType = CommandType.StoredProcedure,
                Connection = sqlconn
            };

            sqlcmd.Parameters.Add("@empolyeeLoginName", SqlDbType.NVarChar, 16);
            sqlcmd.Parameters["@empolyeeLoginName"].Value = loginName;

            sqlcmd.Parameters.Add("@url", SqlDbType.NVarChar, 256);
            sqlcmd.Parameters["@url"].Value = url;

            try
            {
                sqlcmd.Connection.Open();

                sqlrd = sqlcmd.ExecuteReader();

                return sqlrd.HasRows;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                sqlcmd.Connection.Close();
            }
        }
    }

}
